package com.qf.web.controller;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    /*预览订单*/
    //http://localhost:8080/orderservlet?method=getOrderView 预览订单
    public  String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录  登录了才能得到用户id
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //2调用业务逻辑
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //3获取地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList=addressService.find(user.getId());//用户的所有地址所以是集合
            //存储数据
            request.setAttribute("cartList",cartList);
            request.setAttribute("addList",addList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","预览订单失败"+e.getMessage());
            return "/message.jsp";
        }
    }
    /*添加订单*/
    //orderservlet?method=addOrder&aid=7
    public  String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录  登录了才能得到用户id
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //获取前端数据
        String aid = request.getParameter("aid");
        //非空验证
        if (StringUtils.isEmpty(aid)) {
            request.setAttribute("msg","地址不能为空");
            return "/message.jsp";
        }
        try {
            //调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //添加订单先产生一个一个订单 Order order=new Order order的属性还得一个一个找见
            //3.1从购物车中获取money
            CartService cartService=new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            if (cartList == null||cartList.size()==0) {
                request.setAttribute("msg","购物车不能为空");
                return "/message.jsp";
            }
            BigDecimal money=new BigDecimal(0);
            for (Cart cart : cartList) {
                money=money.add(cart.getMoney());
            }
            //3.2创建一个订单号
            String oid = RandomUtils.createOrderId();
            //状态 1未支付 2已支付,代发货 3已发货,待收货 4订单完成
            //时间就是当前时间，aid是前端给的，string所以需要转为Integer
            Order order=new Order(oid,user.getId(),money,"1",new Date(),Integer.parseInt(aid));
            orderService.submitOrder(order,cartList);//carList获取商品信息，因为业务层除了添加订单还需要在订单中添加订单子项 订单细节表里边需要pid，只有购物车有pid
            request.setAttribute("order",order);//得存一下 order.jsp要呢
            return "orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","添加订单失败");
            return "/message.jsp";
        }
    }
    /**
     * 获取订单列表
     * */
    //orderservlet?method=getOrderList
    public  String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录  登录了才能得到用户id
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            List<Order> orderList = orderService.find(user.getId());
            //存值
            request.setAttribute("orderList",orderList);
            System.out.println(orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询订单失败"+e.getMessage());
            return "/message.jsp";
        }
    }
    /*获取订单详情信息*/
    //http://localhost:8080/orderservlet?method=getOrderDetail&oid=202404191707045081371854
    public  String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录  登录了才能得到用户id
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //获取前端数据
        String oid = request.getParameter("oid");
        //非空验证
        if (StringUtils.isEmpty(oid)) {
            request.setAttribute("msg","订单编号不能为空");
            return "/message.jsp";
        }
        try {
            //调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //3.1查看订单
            Order order = orderService.findByOid(oid);//订单编号找订单只能找到一个
            //3.2订单详情 一个订单有多个商品，一个订单号能找到多个商品，返回集合
            List<OrderDetail> orderDetails = orderService.findDetail(oid);
            //存值
            request.setAttribute("order",order);
            request.setAttribute("orderDetails",orderDetails);
            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询订单详情失败"+e.getMessage());
            return "/message.jsp";
        }
    }
    public String getAllOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        OrderService orderService=new OrderServiceImpl();
        List<Order> orders= orderService.findAll();
        request.setAttribute("orderList",orders);
        return "/admin/showAllOrder.jsp";
    }
    public String searchOrder(HttpServletRequest request, HttpServletResponse response)throws Exception
    {
        String username = request.getParameter("username");
        String status = request.getParameter("status");
        OrderService orderService=new OrderServiceImpl();
        List<Order> orders= orderService.findAll();
        List<Order> neworders=new ArrayList<>();
        List<Order> new2orders=new ArrayList<>();
        if(!StringUtils.isEmpty(username))
        {
            for (Order order : orders) {
                if(order.getAddress().getName().contains(username))
                {
                    neworders.add(order);
                }
            }
        }else
        {
            neworders=orders;
        }
        if (!StringUtils.isEmpty(status))
        {
            for (Order neworder : neworders) {
                if(neworder.getStatus()==status)
                {
                    new2orders.add(neworder);
                }
            }
        }else
        {
            new2orders=neworders;
        }
        request.setAttribute("orderList",new2orders);
        return "/admin/showAllOrder.jsp";
    }

}


